const orderModel = require('../model/orderModel');

module.exports.find = async function(searchObj,page){
    // 分页查询
    let {current,size} = page;
    // 获取数据总数
    let total = await orderModel.countDocuments(searchObj)
    // 根据总数/每页显示数得到总页数
    let totalPage = Math.ceil(total / size);
    // limit是每页显示数，skip通过(当前页-1) * 每页显示得到
    let rows = await orderModel
                .find(searchObj)
                .limit(parseInt(size))
                .skip((current - 1) * size);
    return {
        current,size,total,totalPage,rows
    }
}

module.exports.findAll = async function(year){
    console.log({createTime:{$regex:"^" + year}});
    return await orderModel.find({createTime:{$regex:"^" + year}});
}
module.exports.findById = async function(id){
    return await orderModel.findById(id);
}
module.exports.insert = async function(orders){
    return await orderModel.create(orders);
}
module.exports.update = async function(id,orders){
    return await orderModel.updateOne({_id:id},orders);
}
module.exports.delete = async function(id){
    return await orderModel.deleteOne({_id:id});
}